//
//  NotificationViewController.m
//  CommentPage
//
//  Created by 欧杨 on 2020/12/3.
//

#import "NotificationViewController.h"
#import "NotificationTableViewCell.h"
#import "NotificationListViewModel.h"
#import "NotificationCellViewModel.h"
#import <Masonry/Masonry.h>

static void *isReady = &isReady;

@interface NotificationViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(strong, nonatomic) NotificationListViewModel *viewModel;
@property(strong, nonatomic) UITableView *tableView;

@end

@implementation NotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.viewModel = [[NotificationListViewModel alloc] init];
    [self.viewModel addObserver:self forKeyPath:@"isReady" options:NSKeyValueObservingOptionNew context:isReady];
    [self.viewModel getNotifications];
    [self setupSubviews];
}

- (void)setupSubviews
{
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    self.tableView.estimatedRowHeight = 100;
}

#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"numberOfRowsInSection = %lu", (unsigned long)self.viewModel.notifications.count);
    return self.viewModel.notifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"indexPath.row = %ld", (long)indexPath.row);
    NSString *reuseIdentifier = [NSString stringWithFormat:@"%@%ld", @"NotificationTableViewCell", (long)indexPath.row];
    NotificationCellViewModel *cellViewModel = self.viewModel.notifications[indexPath.row];
    NotificationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    //if (cell != nil) [cell removeFromSuperview];
    if (cell == nil) {
        cell = [[NotificationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    [cell bindViewModel:cellViewModel];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel.notifications[indexPath.row] read];
}

#pragma mark -KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    //when isReady change
    if (context == isReady) {
        if ([[change valueForKey:NSKeyValueChangeNewKey] boolValue]) {
            [self setupSubviews];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
}

#pragma mark -REFRESH
- (void) handleRefresh:(id)paramSender
{
  //重新获取已更改的数据源
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.refreshControl endRefreshing];  //结束刷新，即隐藏刷新控件
  //重新加载表
    
}

@end
